package com.bridgeintelligent.tag.webserver.security.session;

import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.apache.commons.lang3.StringUtils;

@Slf4j
public class UserSessionManager {

    private static final Map<String, String> userSessionMap = new ConcurrentHashMap<String, String>();
    private static final Map<String, String> sessionUserMap = new ConcurrentHashMap<String, String>();
    private static UserSessionManager userSessionManager;
    private ReentrantReadWriteLock reentrantLock = new ReentrantReadWriteLock();
    private UserSessionManager(){}

    public static synchronized UserSessionManager newInstance(){
        if (userSessionManager == null){
            userSessionManager = new UserSessionManager();
        }
        return userSessionManager;
    }

    public boolean addSession(String userId, String sessionId){
        try {
            reentrantLock.writeLock().lock();
            userSessionMap.put(userId, sessionId);
            sessionUserMap.put(sessionId, userId);
            return true;
        }catch (Exception e){
            log.error("AddUserSessionErr.err=" + e);
            return false;
        }finally {
            reentrantLock.writeLock().unlock();
        }
    }

    public boolean removeSession(String userId, String sessionId){
        try {
            reentrantLock.writeLock().lock();
            if (userId != null && !userId.isEmpty()){
                String oldSessionId = userSessionMap.get(userId);
                if ( StringUtils.isNotBlank(userId) ) userSessionMap.remove(userId);
                if ( StringUtils.isNotBlank(oldSessionId) ) sessionUserMap.remove(oldSessionId);
            }else {
                String oldUserId = sessionUserMap.get(sessionId);
                if ( StringUtils.isNotBlank(oldUserId) ) userSessionMap.remove(oldUserId);
                if ( StringUtils.isNotBlank(sessionId) ) sessionUserMap.remove(sessionId);
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            log.error("removeUserSessionErr.err=" + e);
            return false;
        }finally {
            reentrantLock.writeLock().unlock();
        }
    }

    public String getSession(String userId){
        try {
            reentrantLock.readLock().lock();
            String sessionId = userSessionMap.get(userId);
            return sessionId;
        }catch (Exception e){
            log.error("getUserSessionErr.err=" + e);
            return null;
        }finally {
            reentrantLock.readLock().unlock();
        }
    }

    public boolean matchSession(String userId, String sessionId){
        try {
            reentrantLock.readLock().lock();
            String oldSessionId = userSessionMap.get(userId);
            if (sessionId.equals(oldSessionId)){
                return true;
            }
            return false;
        }catch (Exception e){
            log.error("matchUserSessionErr.err=" + e);
            return false;
        }finally {
            reentrantLock.readLock().unlock();
        }
    }
}
